package com.android.rapidcash.fragments

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.lib.base.baseclass.BaseApplication
import com.android.lib.base.baseclass.BaseVMFragment
import com.android.lib.base.ext.*
import com.android.lib.base.manager.LiveBusManager
import com.android.rapidcash.activitys.LoginActivity
import com.android.rapidcash.activitys.PerInfoActivity
import com.android.rapidcash.activitys.WebActivity
import com.android.rapidcash.adapters.HomeAdapter
import com.android.rapidcash.utils.CommUtils
import com.android.rapidcash.viewmodel.MainViewModel
import com.arc.fast.span.appendFastTextStyle
import com.arc.fast.span.enableClickableSpan
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.R
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.databinding.FragmentHomeBinding


class HomeFragment : BaseVMFragment<MainViewModel, FragmentHomeBinding>() {
    private val mAdapter by lazy { HomeAdapter() }
    private var isNeedRefresh = false

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.tv2.chip(40)
        mBinding.tv4.chip(40)
        mBinding.ll2.chipTop(20)
        mBinding.iv2.chip(10)
        mBinding.rvList.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            adapter = mAdapter
            mAdapter.addData("0")
            mAdapter.addData("1")
        }
        initString()
    }

    fun refresh() {
        if (!isNeedRefresh) return
        viewModel.index()
    }

    override fun lazyLoadData() {
        isNeedRefresh = true
        viewModel.index()
    }

    override fun initListener() {
        mBinding.rlRefresh.setOnRefreshListener {
            mBinding.rlRefresh.finishRefresh(400)
            viewModel.index()
        }
        mBinding.tv4.click {
            if (CommUtils.isLogin()) {
                startActivity(Intent(requireContext(), PerInfoActivity::class.java).apply {
                })
            } else {
                startActivity(Intent(requireContext(), LoginActivity::class.java).apply {
                    putExtra("loginToPage", 2)
                })

            }
        }

        LiveBusManager.observeInt(this, "Login") {
            refresh()
        }
    }


    override fun initObserve() {
        viewModel.index.observe(this) {
            mBinding.tv5.text =
                CommUtils.FormatNumber(it.festivalBusyPrisonerEgyptianPenny.toString())
        }
    }

    private fun initString() {
        val spannableStringBuilder = SpannableStringBuilder()

        spannableStringBuilder.append(getString(R.string.home_1) + " ")

        spannableStringBuilder.appendFastTextStyle(
            "<${getString(R.string.home_2)}>"
        ) {
            textColor = Color.parseColor("#FE5A59")
            onClick = {
                startActivity(Intent(BaseApplication.getContext(), WebActivity::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    putExtra("url", "https://www.baidu.com")
                    putExtra("title", getString(R.string.home_2))
                })
            }
        }
        mBinding.tvInfo.text = spannableStringBuilder
        mBinding.tvInfo.enableClickableSpan() // 启用点击事件
    }


    companion object {
        @JvmStatic
        fun newInstance() = HomeFragment()
    }
}